module Regular::LatexTableFormatter

import Number;
import String;
import Main;
import IO;
import Constants;
import List;

/*data EssayMetrics = essaymetrics(
		str packageName, 
		str className, 
		int moduleSize,
		int totalCC, 
		real ccPerNLOC, 
		int totalAdjustedCC, 
		real adjustedCCPerNLOC);*/

public str formatResultToLatexTable(list[EssayMetrics] metrics){
	str tableStart = "\\begin{tabular}{ |l|l|l|l|l|l|} \\hline \n";
	str tableHeader = "\\textbf{Class Name} & \\textbf{Module Size} & \\textbf{Total CC} & \\textbf{Total Adjusted CC} & \\textbf{CC per <Constants::PERLOCAMOUNT>} & \\textbf{Ajusted CC per <Constants::PERLOCAMOUNT>} \\\\ \\hline \n";
	str tableContent = "";
	for ( EssayMetrics metric <- metrics){
		// Cant do it nicely as findLast does not exists anymore.
		list[int] positionOfPoint = findAll(metric.className, ".");
		int lastPosition = positionOfPoint[size(positionOfPoint)-1];
		str onlyClassName = substring(metric.className, lastPosition+1);
		println("position of .: <positionOfPoint> String before <metric.className> String after <onlyClassName>");
		tableContent = tableContent + "<onlyClassName> & <metric.moduleSize> & <metric.totalCC> & <metric.totalAdjustedCC> & <truncateReal(metric.ccPerNLOC,2)> & <truncateReal(metric.adjustedCCPerNLOC,2)> \\\\ \\hline \n";
		
	}
	str tableCaption = "\\caption{Raw Complexity Metrics} \\label{rawccmetrics}\n";
	str tableEnd = "\\end{tabular}\n";
	str endResult = tableStart + tableHeader + tableContent + tableEnd;
	println(endResult);
	return endResult;
}

public void printResultToStringTable(list[EssayMetrics] metrics){
	for ( EssayMetrics metric <- metrics){
		println("ClassName: <metric.className>");
		println("\tModule Size (LoC):\t\t<metric.moduleSize>");
		println("\tTotal CC:\t\t\t<metric.totalCC>");
		println("\tCC per <Constants::PERLOCAMOUNT> LOC:\t\t<truncateReal(metric.ccPerNLOC,2)>");
		println("\tTotal Adjusted CC:\t\t<metric.totalAdjustedCC>");
		println("\tAdjusted CC per <Constants::PERLOCAMOUNT> LOC:\t<truncateReal(metric.adjustedCCPerNLOC,2)>");
		println("");
	}
}


// Function to truncate a Real. Not as nice as real rounding, but works
// for this purpose. Heavily inspired by Jurgen Vinju's impl from
// http://tutor.rascal-mpl.org/Courses/Rascal/Rascal.html#/Courses/Rascal/Expressions/Values/Real/Real.html
private real truncateReal(real number, int decimalPoints) {
	if (/<prefix:\d+\.\d{<decimalPoints>}>/ := "<number>") return toReal(prefix);
	else return number;
}